
<template>
  <div class="container">
    <div class="condition-box">
      <el-form :inline="true" :model="condition" class="demo-form-inline">
        <el-form-item label="房源id">
          <el-input type="text" v-model="condition.houseid" placeholder="请输入房源编码" size="mini" clearable>
          </el-input>
        </el-form-item>
      <el-form-item label="房源编码">
      <el-input type="text" v-model="condition.code" placeholder="请输入房源编码" size="mini" clearable>
      </el-input>
      </el-form-item>
      <el-form-item label="房源状态">
      <el-select v-model="condition.status" type="text" placeholder="请选择状态码" size="mini" clearable>
        <el-option
                v-for="s in states"
                :key="s.value"
                :label="s.text"
                :value="s.value">
        </el-option>
      </el-select>
      </el-form-item>
        <el-form-item label="房源地址">
          <el-input type="text" v-model="condition.address" placeholder="请输入房源编码" size="mini" clearable>

          </el-input>
        </el-form-item>
      <el-button type="primary" @click="search()" size="mini">查询</el-button>
      <el-button type="default" @click="reset()" size="mini">重置</el-button>
      <el-button type="danger" @click="addhouse()" size="mini">新增</el-button>
      </el-form>
    </div>
    <div class="tablef">
      <el-table :data="tableData" height="400px" border size="mini"  style="width: 100%; text-align: center;">
        <el-table-column prop="houseid" label="房源id"></el-table-column>
        <el-table-column prop="houseCode" label="房源编号">
        </el-table-column>
        <el-table-column prop="houseType" label="房源信息">
        </el-table-column>
        <el-table-column prop="status" label="房源状态">
          <template scope="scope">
            {{getDict(scope.row.status)}}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="房源地址">
        </el-table-column>
        <el-table-column label="操作"  width="200">
          <template slot-scope="scope">
            <el-button type="text" size="mini"  @click="editHouse(scope.row)">修改信息</el-button>
            <el-button type="text" size="mini"  @click="deleteHouse(scope.row.houseid)">删除</el-button>
            <el-button type="text" size="mini"  @click="imgeHouse(scope.row)">查看图片</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination small background
                   :page-sizes="[5,10,15,20]"
                   layout="total,sizes,prev,pager,next"
                   :current-page="currentPage"
                   :page-size="displayCount"
                   :total=total
                   @current-change="changeCurrentPage"
                   @size-change="changeSize"
    >
    </el-pagination>
    <el-dialog :title="title" :visible.sync="addvisible"
               width="45%" @close="closeDialog">
      <el-form :model="add" :rules="houseRules" :inline="true" v-show="title=='新增'">
        <el-form-item label="房源编号" prop="housecode" label-width="100px">
          <el-input v-model="add.houseCode" size="mini" clearable placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="房源信息" prop="housecode" label-width="100px">
          <el-input v-model="add.houseType" size="mini" clearable placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="房源地址" prop="housecode" label-width="100px">
          <el-input v-model.number="add.address" size="mini" clearable placeholder="请输入房源地址"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="100px">
          <el-input v-model="add.image" size="mini" clearable placeholder="请输入图片地址"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="edit" :rules="houseRules" :inline="true" v-show="title=='修改'">
        <el-form-item label="房源编号" prop="housecode" label-width="100px" disabled>
          <el-input v-model="edit.houseCode" size="mini" clearable placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="房源信息" prop="housecode" label-width="100px">
          <el-input v-model="edit.houseType" size="mini" clearable placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="房源地址" prop="housecode" label-width="100px">
          <el-input v-model.number="edit.address" size="mini" clearable placeholder="请输入房源地址"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="100px">
          <el-input v-model="edit.image" size="mini" clearable placeholder="请输入图片地址"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog" size="small">取 消</el-button>
        <el-button type="primary" @click="saveHouse" size="small">保 存</el-button>
      </div>
    </el-dialog>
    <el-dialog title="图片查看" :visible.sync="imagevisible"
               width="45%" @close="closeDialog">
      <div>
        <img :src="edit.image" style="width: 70%" height="60%">
      </div>

    </el-dialog>
  </div>
</template>

<script>
import {houseSearch, houseAdd, houseUpdate,houseDelete} from "../../api/user";
export default {
  //页面创建时调用
  data() {

    return {
      imagevisible:false,
      currentPage:1,
      displayCount:5,
      total:0,
      states:[
        {value:0,text:'闲置中'},
        {value:1,text:'租用中'},

      ],
      house:[],
      houseRules:{
        housecode: [
          {required: true, message: '请输入', trigger: 'blur'},
        ],
      },
      /*查询条件*/
      condition:{
        houseid:'',
        code:'',
          status:null,
        address:'',
        page:null,
        limit:null,
      },
      title:'',
      addvisible:false,
      edit:{},
      add:{
        code:'',
        houseType:'',
        address:'',
        status:0,
        image:'',
      },
      tableData:[],
      statusList:[{name: '使用中',value: 0},{name:'未使用',value: 1}],

    }
  },
  mounted() {
    this.search()
  },
  methods:{
    search(){
      houseSearch(
              this.condition,
              this.condition.page=this.currentPage,
      this.condition.limit=this.displayCount
      ).then((res)=>{
        this.tableData = res.data;
        this.total = res.count ;
      })
    },

    deleteHouse(houseid){
      let flag = confirm("确定删除?");
      if(flag){
        houseDelete({houseid}).then((data)=>{
          if(data.data == null){
            this.$message({
              message:'有订单正在进行，删除失败！',
              type:'error'
            })
            this.search();
          }
          else {
            this.$message.success('删除成功！')
            this.search();
          }
        })
      }

    },
    editHouse(house){
      this.title='修改'
      this.addvisible=true;
      this.edit={...house};
    },
    imgeHouse(house){
      this.imagevisible=true;
      this.edit={...house};
    },
    reset(){
      this.condition ={
        inputcode:'',
        status:undefined,

      }
    },
    getDict(data){
      if(data==0){
        return '闲置中'
      }else {
        return '租用中'
      }
    },
    saveHouse(){
      if(this.title=='新增'){
        houseAdd(
                this.add
        ).then((res)=>{
          if(res.msg=='OK'){
            this.$message.success('新增成功!')
            this.addvisible=false;
            this.search()
          }else {this.$message.error('新增失败!')}
        })
      }else {
        houseUpdate(
                this.edit
        ).then((res)=>{
          if(res.msg=='OK'){
            this.$message.success('修改成功!')
            this.addvisible=false;
            this.search()
          }else {this.$message.error('修改失败!')}
        })
      }
    },
    addhouse(){
      this.title='新增'
      this.addvisible=true;
    },
    closeDialog () {
      this.add={
        houseCode:'',
                houseType:'',
                address:'',
                status:1,
                image:'',
      },
      this.addvisible = false;
    },
    /* 条件查询*/
    handlerCelectionChange(){

    },
    //当前页码改变触发
    changeCurrentPage(val){
      this.currentPage = val ;
      this.search();
    },
    //每页展示的记录数发生改变
    changeSize(val){
      this.displayCount = val ;
      this.search();
    },

  }

}
</script>
<style>
  .content-box{
    width: calc(100% - 10px);
    height: 100%;
  }
  .condition-box, .button-box{
    height: 40px;
    line-height: 40px;
    color: #909399;
    font-size: 12px;
    margin: 10px;
  }
  .tablef {
    flex: 8.25;
    border-radius: 0px;
    box-shadow: rgba(0, 0, 0, 0.047) 0px 3px 5px;
  }
  .condition-box{
    color: #909399;
    font-size: 12px;
  }
  .el-pagination{
    height: 30px;
    padding-top: 10px;
  }
  .el-input, .el-select{
    width: 120px;
    margin-right: 10px;
  }
  .el-pagination .el-select .el-input > input{
    height: 22px;
  }

  .el-pagination > span{
    float: left;
  }
</style>